//
//  ActivityViewController.m
//  Fengly_Nuo_Movie
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 Fengly_Nuo. All rights reserved.
//

#import "ActivityViewController.h"
#import "ActivityTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "ActivityModel.h"
#import "ActivityParticularsViewController.h"
#import "UserCollectViewController.h"
#import "UserViewController.h"
#import "Fengly_nuo_SQLDataBaseHandle.h"

// MARK:VIEW_CENTER_X:屏中心x轴
#define VIEW_CENTER_X self.view.center.x

// MARK:VIEW_CENTER_Y:屏中心y轴
#define VIEW_CENTER_Y self.view.center.y

// MARK:HEIGHT:屏高
#define HEIGHT self.view.frame.size.height

// MARK:WIDTH:屏宽
#define WIDTH self.view.frame.size.width

@interface ActivityViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *arrModels;

@property (nonatomic, assign) NSInteger count;

@end

@implementation ActivityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 界面设置
    [self createView];
    
    // 数据处理
    [self dataHandle];
    
    // 收藏数组
    self.arrCollect = [NSMutableArray array];
}



#pragma mark - UITableView Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _arrModels.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ActivityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
   
    ActivityModel *model = _arrModels[indexPath.row];
    
    cell.model = model;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 220;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ActivityParticularsViewController *activityParticularsVC = [[ActivityParticularsViewController alloc] init];
    
    activityParticularsVC.model = _arrModels[indexPath.row];
    
//    // 求label 高度
//    // 文字的大小，描述文字大小的信息
//    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], NSFontAttributeName, nil];
//    // 通过文字大小 获取文本的高度
//    ActivityModel *model = _arrModels[indexPath.row];
//    CGRect rect = [model.content boundingRectWithSize:CGSizeMake(WIDTH - 40, 0) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dic context:nil];
//    // 属性传值 将label高度给 bigLabelHelght
//    activityParticularsVC.bigLabelHelght = rect.size.height;
    
    [self.navigationController pushViewController:activityParticularsVC animated:YES];
    [activityParticularsVC release];
}



// 界面设置
- (void)createView {
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"活动";
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 49) style:(UITableViewStylePlain)];
    [self.view addSubview:_tableView];
    [_tableView release];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [_tableView registerClass:[ActivityTableViewCell class] forCellReuseIdentifier:@"cell"];
}

// 数据处理
- (void)dataHandle {
    // 获取文件路径
    NSString *path = [[NSBundle mainBundle] pathForResource:@"activitylist" ofType:@"txt"];
    // 转换为data数据
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSError *error = nil;
    // JSON解析
    id result = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:&error];
    if (error) {
        NSLog(@"%@", error);
    }
    NSDictionary *dataDic = result;
    NSMutableArray *arrEvents = [NSMutableArray arrayWithArray:dataDic[@"events"]];
    
    self.arrModels = [NSMutableArray array];
    for (NSDictionary *dic in arrEvents) {
        ActivityModel *model = [ActivityModel activityModel];
        [model setValuesForKeysWithDictionary:dic];
        [_arrModels addObject:model];
    }
    
    // 创建表
    [[Fengly_nuo_SQLDataBaseHandle sharedDataBaseHandle] createTableWithTableName:@"activityList"];
}



- (void)dealloc {
    [_arrCollect release];
    [_tableView release];
    [_arrModels release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
